import os, sys
try:
  from docx import Document
except:
  os.system("pip3 install --user python-docx")
  from docx import Document

def main(a):
    s= Document()
    s3= sorted(os.listdir(a))
    for i in s3:
      if i[-4:]!=".txt":
        continue
      i = os.path.join(a, i)
      with open(i, mode='r', encoding='utf8') as s5:
        s6= s5.read() # 这里必须为静态引用否则add_paragraph不能引用
        s4= s.add_paragraph(s6)
      s.add_page_break()
    s.save("%s.docx" %os.path.basename(a))

if __name__ == "__main__":
  if len(sys.argv[1:]) ==0:
    print("将多个txt文件转换为多页doc。请把txt文件放入文件夹中并将此文件夹作为参数")
    sys.exit()
  for i in sys.argv[1:]:
    if not os.path.isdir(i):
      print("请把txt文件放入文件夹中并将此文件夹作为参数")
      sys.exit()
    main(i.strip('\'\"\\').rstrip("/"))
